<?php

namespace App\Http\Controllers\Mobile\Company;
use App\Facades\Style;
use App\Http\Requests\Company\Department\DepartmentCreateRequest;
use App\Http\Requests\Company\Department\DepartmentUpdateRequest;
use App\Models\Company\CompanyDepartment;
use App\Models\Company\CompanyBasic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DepartmentController extends CommonController{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request){
        $user = auth()->guard('company')->user();
        $keyword=$request->input('keyword');
        //获取面试通知记录
        $department_list = DB::table('company_department')
            ->select(
                'id','name','contacts','mobile','phone','updated_at'
            )
            ->where('uid',$user->uid)
            ->where(function ($query) use ($keyword) {
                $query->where('name', 'like', '%'.$keyword.'%')->orWhere('contacts','like','%'.$keyword.'%');
            })
            ->orderBy('updated_at', 'desc')
            ->paginate(10);

        foreach($department_list as $key=>$v){
            $v->phone=$v->phone?$v->phone:$v->mobile;
            $v->updated_at = substr($v->updated_at,0,16);
        }

        $input=arrayFilter(
            array('keyword'=>$keyword)
        );

        return Style::view('mobile.company.departmentIndex',compact('department_list','input'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return Style::view('mobile.company.departmentCreate');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(DepartmentCreateRequest $request){
        $user = auth()->guard('company')->user();
        $input = $request->only('name', 'contacts', 'mobile', 'phone', 'fax', 'email');
        //获取用户信息ID
        $input['uid'] = $user->uid;
        $companyBasic = CompanyBasic::select('id')->where('uid',$user->uid)->orderBy('updatetime', 'desc')->first();
        $input['com_id'] = $companyBasic->id;
        //dd($input);
        $id=CompanyDepartment::insertGetId($input);

        if($request->ajax()){
            return response()->json(['status' => 200,'id'=>$id,'name' => $input['name']]);
        }else{
            return redirect('mobile/company/department')->with('ok',"添加成功");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = auth()->guard('company')->user();
        $CompanyDepartment=CompanyDepartment::where('uid',$user->uid)->find($id);
        return Style::view('mobile.company.departmentEdit',compact('CompanyDepartment'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(DepartmentUpdateRequest $request, $id){
        $user = auth()->guard('company')->user();
        $input = $request->only('name', 'contacts', 'mobile', 'phone', 'fax', 'email');
        CompanyDepartment::where('id',$id)->where('uid',$user->uid)->update($input);
        return redirect('mobile/company/department')->with('ok',"更新成功");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request,$id){
        $user = auth()->guard('company')->user();
        if($id and $id!=0){
            $deletedRows = CompanyDepartment::where('id', $id)->where('uid',$user->uid)->delete();
            if($deletedRows){
                return response()->json(['status' => 200,'message'=>"删除部门成功"]);
            }else{
                return response()->json(['status' => 404,'message'=>"删除部门失败"]);
            }
        }elseif($id==0 and is_array($request->selected_id)){
            $deletedRows = CompanyDepartment::whereIn('id', $request->selected_id)->where('uid',$user->uid)->delete();
            if($deletedRows){
                return back()->withInput();
            }
        }
        //return back()->withInput();
    }
}
